import { usePlcWithOut } from '@/store/modules/plc';

const plcStore = usePlcWithOut();

export const setListFn = (list: any = []) => {
  if (list && list.length) {
    const newList: any = [];

    for (const item of list) {
      if (item.list) {
        item.list = setListFn(item.list);

        newList.push(item);
      } else {
        newList.push({
          inputType: null, // 读、写、读写、确认写入、0/1按钮
          labelZh: null, // 中文
          labelEn: null, // 英文
          labelTrueZh: null, // 1中文
          labelTrueEn: null, // 1英文
          labelFalseZh: null, // 0中文
          labelFalseEn: null, // 0英文
          realityLabelZh: null, // 实际值中文
          realityLabelEn: null, // 实际值英文
          setupLabelZh: null, // 设置值中文
          setupLabelEn: null, // 设置值英文
          top: null, // 定位top值
          left: null, // 定位left值
          funId: null, // 唯一标识id
          hiddenTrueFunIdList: [], // 1隐藏其他框
          hiddenFalseFunIdList: [], // 0隐藏其他框
          writeVal: null, // 写入的值
          updateClear: false, // 写入后清空
          updateSetVal: null, // 写入后设置value的值
          updateSetFunId: null, // 写入后设置该id的值
          updateClearFunId: null, // 写入后清空该id的值
          defaultValGetFunId: null, // 默认值为该id读到的值
          defaultValGetLocal: false, // 默认值为该id存储到localstroage的值
          valSetLocal: false, // 写入的值是否实时写入到localstroage
          defaultVal: 0, // 默认值
          blurChange: false, // 失去焦点是否进行写入
          hidden: false, // 隐藏
          img: null, // 图片
          imgTrue: null, // 1图片
          imgFalse: null, // 0图片
          styleType: null, // 样式类型
          min: -10000000000, // 输入最小值
          max: 10000000000, // 输入最大值
          sliderMin: 0, // slider最小值
          sliderMax: 100, // slider最大值
          step: 1, // slider步长
          lengFunIdObj: null, // 楞别funId对象
          ...item
        });
      }
    }

    return newList;
  }

  return list;
};

export const numMap = {
  0: '零',
  1: '一',
  2: '二',
  3: '三',
  4: '四',
  5: '五',
  6: '六',
  7: '七',
  8: '八',
  9: '九',
  10: '十'
};

export const numEnMap = {
  0: 'Zero',
  1: 'One',
  2: 'Two',
  3: 'Three',
  4: 'Four',
  5: 'Five',
  6: 'Six',
  7: 'Seven',
  8: 'Eight',
  9: 'Nine',
  10: 'Ten'
};

export const topList = [
  '-8.38%',
  '8.52%',
  '42.23%',
  '60.08%',
  '92.86%',
  '26.38%'
  // '624px', '702px'
];

export const leftList = [
  '7.38%',
  '31.4%',
  '43.81%',
  '53.97%',
  '77.78%',
  '80.78%',
  '100.78%'
  // '950px',
  // '970px',
  // '1020px',
  // '1200px',
];

const getNumValFn = (obj: any = null, list: any = []) => {
  let val: any = 0;

  if (obj) {
    const {
      funId = null,
      computedValInListObj = null,
      value = null,
      decimalPlaces = 0,
      precision=null
    } = obj;

    // if (computedValInListObj) {
    //   val = getComputedValFn(computedValInListObj, list);
    // }

    if (value) {
      val = value;
    } else if (computedValInListObj) {
      val = getComputedValFn(computedValInListObj, list);
    } else if (funId && list && list.length) {
      const obj: any = list.find(item => item.funId === funId);

      if (obj) {
        val = obj.value || 0;
      } else {
        val = plcStore.newData[funId] || 0;

        // 小数位
        if (decimalPlaces !== 0) {
          // console.log('decimalPlaces')
          // console.log(decimalPlaces)
          let zero = '';

          for (let i = 0; i < decimalPlaces; i++) {
            zero += '0';
          }

          const num = Number(1 + zero);

          if (!isNaN(num)) {
            val = Number(val / num).toFixed(
              precision !== null ? precision : precision || 2
            );
          }
        }
      }
    }


  }

  return isNaN(Number(val)) ? 0 : Number(val);
};

export const getComputedValFn = (
  computedValInListObj: any = null,
  list: any = []
) => {
  // console.log('计算')

  let val: any = 0;

  if (computedValInListObj) {
    const {
      num1 = null,
      num2 = null,
      computedType = null
    } = computedValInListObj;

    let num1Val: any = 0;

    let num2Val: any = 0;

    if (computedType) {
      if (num1) {
        num1Val = getNumValFn(num1, list);
      }

      if (num2) {
        num2Val = getNumValFn(num2, list);
      }

      switch (computedType) {
        case 'add':
          console.log('加');

          console.log('num1Val', num1Val);

          console.log('num2Val', num2Val);

          val = (num1Val * 100 + num2Val * 100)/100;

          break;

        case 'reduce':
          console.log('减');

          console.log('num1Val', num1Val);

          console.log('num2Val', num2Val);

          val = (num1Val * 100 - num2Val * 100)/100;

          break;

        case 'ride':
          console.log('乘');

          console.log('num1Val', num1Val);

          console.log('num2Val', num2Val);
          val = ((num1Val * 100) * (num2Val * 100))/10000;

          break;

        case 'divided':
          console.log('除');

          console.log('num1Val', num1Val);

          console.log('num2Val', num2Val);
          console.log('num1Val', num2Val);
          console.log('num2Val', num2Val);

          val = ((num1Val * 100) / (num2Val * 100));

          break;

        default:
          break;
      }

      console.log(val);

      console.log(list);

      // console.log(num1Val);

      // console.log(num2);

      // console.log(num2Val);
    }
  }

  return (val).toFixed(2);
};
